package com.zz.nondatain.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityMyAppealBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的申诉页面，显示用户申诉记录
 */
public class MyAppealActivity extends BaseActivity<ActivityMyAppealBinding> {

    private List<AppealRecord> appealRecords = new ArrayList<>();
    private AppealAdapter adapter;

    @Override
    protected void initView() {
        // 设置RecyclerView
        binding.recyclerAppeals.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AppealAdapter(appealRecords);
        binding.recyclerAppeals.setAdapter(adapter);
        
        // 加载申诉记录
        loadAppealRecords();
    }

    @Override
    protected void initData() {
        // 初始化数据在loadAppealRecords中完成
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityMyAppealBinding getViewBinding() {
        return ActivityMyAppealBinding.inflate(getLayoutInflater());
    }
    
    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 添加申诉按钮点击事件
        binding.btnAddAppeal.setOnClickListener(v -> {
            // 跳转到添加申诉页面
            Intent intent = new Intent(this, AddAppealActivity.class);
            startActivity(intent);
        });
    }
    
    /**
     * 加载申诉记录
     */
    private void loadAppealRecords() {
        // 清空现有记录
        appealRecords.clear();
        
        // 添加模拟数据 - 实际应该从API或数据库获取
        appealRecords.add(new AppealRecord(
                "2025年04月14日 09:59:05", 
                "忘打卡",
                "2025年04月13日 00:00:00", 
                "忘记打卡", 
                "审核中"));
                
        // 根据数据状态显示列表或"暂无数据"
        if (appealRecords.isEmpty()) {
            binding.recyclerAppeals.setVisibility(View.GONE);
            binding.layoutNoData.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerAppeals.setVisibility(View.VISIBLE);
            binding.layoutNoData.setVisibility(View.GONE);
        }
        
        // 刷新适配器
        adapter.notifyDataSetChanged();
    }

    /**
     * 申诉记录数据类
     */
    private static class AppealRecord {
        String appealTime;      // 申诉时间
        String appealReason;    // 申诉理由
        String occurrenceTime;  // 发生时间
        String factReason;      // 事实缘由
        String status;          // 申诉状态
        
        AppealRecord(String appealTime, String appealReason, String occurrenceTime, 
                      String factReason, String status) {
            this.appealTime = appealTime;
            this.appealReason = appealReason;
            this.occurrenceTime = occurrenceTime;
            this.factReason = factReason;
            this.status = status;
        }
    }
    
    /**
     * 申诉记录适配器
     */
    private class AppealAdapter extends RecyclerView.Adapter<AppealAdapter.ViewHolder> {
        
        private List<AppealRecord> records;
        
        AppealAdapter(List<AppealRecord> records) {
            this.records = records;
        }
        
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_appeal, parent, false);
            return new ViewHolder(view);
        }
        
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            AppealRecord record = records.get(position);
            holder.tvAppealTime.setText(record.appealTime);
            holder.tvAppealReason.setText(record.appealReason);
            holder.tvOccurrenceTime.setText(record.occurrenceTime);
            holder.tvFactReason.setText(record.factReason);
            holder.tvAppealStatus.setText(record.status);
            
            // 根据状态设置不同的颜色
            switch (record.status) {
                case "审核中":
                    holder.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
                    break;
                case "已通过":
                    holder.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
                    break;
                case "已拒绝":
                    holder.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
                    break;
                default:
                    holder.tvAppealStatus.setTextColor(getResources().getColor(android.R.color.darker_gray));
                    break;
            }
            
            // 点击事件，跳转到申诉详情页面
            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(MyAppealActivity.this, AppealDetailActivity.class);
                // 传递申诉数据
                intent.putExtra("appeal_time", record.appealTime);
                intent.putExtra("appeal_reason", record.appealReason);
                intent.putExtra("occurrence_time", record.occurrenceTime);
                intent.putExtra("fact_reason", record.factReason);
                intent.putExtra("status", record.status);
                
                startActivity(intent);
            });
        }
        
        @Override
        public int getItemCount() {
            return records.size();
        }
        
        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvAppealTime;
            TextView tvAppealReason;
            TextView tvOccurrenceTime;
            TextView tvFactReason;
            TextView tvAppealStatus;
            
            ViewHolder(View itemView) {
                super(itemView);
                tvAppealTime = itemView.findViewById(R.id.tv_appeal_time);
                tvAppealReason = itemView.findViewById(R.id.tv_appeal_reason);
                tvOccurrenceTime = itemView.findViewById(R.id.tv_occurrence_time);
                tvFactReason = itemView.findViewById(R.id.tv_fact_reason);
                tvAppealStatus = itemView.findViewById(R.id.tv_appeal_status);
            }
        }
    }
} 